<?php
/**
 * Created by PhpStrom
 * User: 魏惠东
 * Date: 2021/6/2 15:09
 * Desc:
 */

namespace App\Models\Admin;
use App\Models\BaseModel;
use App\Models\Admin\UserGroupMember as UserGroupMemberModel;
class UserGroup extends BaseModel{
    protected $table = "user_group";
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected $fillable=["name","description","site_id","creator"];
    /**
     * 列表
     * @param $search 条件数组
     */
    public static function getList($limit,$search){
        $res = self::
        orderBy('id','desc')->paginate($limit)->toArray();
        foreach ($res['data'] as $k=>$v){
            $count = 0;
            $search['group_id'] = $v['id'];
            $data = UserGroupMemberModel::selectData($search)->toArray();

            $data = UserGroupMemberModel::calculationNumber($data);
            foreach ($data as $a=>$b){
                $count+=$b['number'];
            }
            $res['data'][$k]['number'] = $count;
        }
        return $res;
    }

    public static function selectData($where){
        return self::where($where)->get();
    }
}
